//
//  AddFriendViewController.m
//  XMPPHelper
//
//  Created by 蔡成汉 on 15/5/8.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "AddFriendViewController.h"
#import "XMPPHelper.h"

@interface AddFriendViewController ()<UITextFieldDelegate>
{
    UILabel *tipLabel;
    
    UITextField *userNameTextField;
}
@end

@implementation AddFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"添加好友";
    
    if ([[UIDevice currentDevice] systemVersion].floatValue>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 100, 200, 30)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.textColor = [UIColor blueColor];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:tipLabel];
    
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 100, 200, 30)];
    userNameTextField.delegate = self;
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    userNameTextField.placeholder = @"用户名";
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:userNameTextField];
    
    UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addFriendButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 200, 200, 30);
    [addFriendButton setTitle:@"添加好友" forState:UIControlStateNormal];
    [addFriendButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [addFriendButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [addFriendButton addTarget:self action:@selector(addFriendButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addFriendButton];
}

/**
 *  添加好友
 *
 *  @param paramSender <#paramSender description#>
 */
-(void)addFriendButtonIsTouch:(UIButton *)paramSender
{
    if (userNameTextField.text != nil && userNameTextField.text.length >0)
    {
        XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
        [myXMPPHelper addFriend:userNameTextField.text];
        [userNameTextField resignFirstResponder];
    }
    else
    {
        UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户名为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [myAlertView show];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
